﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using webapp4U;
using webapp4U.BOL;

namespace webapp4U.UI
{
    public partial class Article : System.Web.UI.Page //BasePage
    {
        int CompanyID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Set_ErrorMessage();
            if (Globals.GetStringFromQueryString("Action") == "Edit")
            {
                CompanyID = webapp4U.UI.BasePage.GetIntFromQueryString("CompanyID");
                btSave.Text = ResourceManager.GetString("Update");
                if (!IsPostBack)
                    Load_CompanyByID();
            }
            else
                imgLogo.Visible = false;
        }

        private void Set_ErrorMessage()
        {
            RegEmail.ErrorMessage = ResourceManager.GetString("Register_Email_error");
        }

        private void Load_CompanyByID()
        {
            webapp4U.BOL.Companies obj = webapp4U.BOL.Companies.GetCompaniesByID(CompanyID);
            txtCompanyName.Text = obj.CompanyName;
            txtAddess.Text = obj.Address;
            txtEmail.Text = obj.Email;
            txtFax.Text = obj.Fax;
            txtPhone.Text = obj.Phone;
            HPhotoFile.Value = obj.Logo;
            txtBody.Text = obj.Description;
            if(obj.Logo.Length >3)
            {
                imgLogo.Visible = true;
                imgLogo.ImageUrl = "~/UserUpload/Logo/" + obj.Logo;
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            Companies obj = new Companies();
            obj.CompanyID = CompanyID;
            obj.CompanyName = txtCompanyName.Text;
            obj.Logo = Photo_Upload();
            obj.Address = txtAddess.Text;
            obj.Email = txtEmail.Text;
            obj.Phone = txtPhone.Text;
            obj.Fax = txtFax.Text;
            obj.AddedDate = DateTime.Now;
            obj.AddedBy = Globals.CurrentUserName;
            obj.Description = txtBody.Text;
            obj.IsActive = true;
            obj.DisplayOrder = 0;

            if (BasePage.GetStringFromQueryString("Action") == "Edit")
            {
                webapp4U.BOL.Companies.Update(obj);
                lblMsg.Text = ResourceManager.GetString("ModifiedSuccessfully");
                Response.Redirect(Globals.ApplicationPath + "WebMaster/Companies/");
            }
            else
            {
                webapp4U.BOL.Companies.Insert(obj);
                Response.Redirect(Globals.ApplicationPath + "WebMaster/Companies/");
            }
        }


        protected string Photo_Upload()
        {
            string strFileName = "";
            // kiem tra xem user co chon hinh dai dien ko
            if (PhotoFile.HasFile)
            {
                // lay ten file de khong bi trung file + phan mo rong cua file can upload
                strFileName = Common.GetFileName() + System.IO.Path.GetExtension(PhotoFile.FileName);

                // kiem tra phan mo rong cua file co hop le hay khong
                string strExtension = System.IO.Path.GetExtension(PhotoFile.FileName).ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" || strExtension == ".PNG" || strExtension == ".BMP")
                {
                    string strFilePath = Request.PhysicalApplicationPath + "/UserUpload/Logo/" + strFileName;

                    //thuc hien save file Large len server
                    EnumUpload uploadLarge = FileProcess.UpLoadImage(PhotoFile, strFilePath, 250);
                }
                else
                {
                    lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                    return strFileName = "";
                }
            }
            else
            {
                return HPhotoFile.Value.Trim();
            }

            return strFileName;
        }

      
    
    }
}